package com.sunrise.visual.frame.common
{
	import mx.controls.Alert;

	public class BarUtil
	{
		public var config:Object;
		public var data:Array;
		
		public function BarUtil(data:Array,config:Object)
		{
			this.data = data;
			this.config = config;
		}
		public function getXML():XML{
			var xml:XML = <anychart>
							<margin all="0" left={this.config.marginLeft || "0"} right={this.config.marginRight || "0"}/>
							<settings>
								<animation enabled="true" />
							</settings>
							<charts>
							<chart plot_type={this.config.plotType || "CategorizedVertical"}>
								<data_plot_settings default_series_type="Bar">
									<bar_series style="Silver" {this.config.groupPadding?'group_padding="' + this.config.groupPadding + '"':""} {this.config.pointPadding?'point_padding="' + this.config.pointPadding + '"':""}>
										<tooltip_settings enabled={this.config.tooltipEnabled || "true"}>
<font family="Microsoft Yahei" bold="false" size="12" />
											<format>{config.tooltipFormat || "{%Name}{numDecimals:0,thousandsSeparator:}\n{%SeriesName}:{%YValue}{numDecimals:2}"}{this.config.format_zbUnit||''}</format>
										</tooltip_settings>
										<label_settings enabled={this.config.labelEnabled || "False"}>
											<position anchor={this.config.label_SettingsAnchor||"CenterTop"} valign={this.config.label_SettingsValign||"Top"} padding="0"/>
<font family="Microsoft Yahei" bold={this.config.labelBold || "false"} size="10" color={this.config.labelColor || "white"}/>
												<format>{config.labelFormat||"{%YValue}{numDecimals:2}"}</format>
													<effects>
													<drop_shadow enabled="true" opacity="1"/>
													</effects>
										</label_settings>
										<bar_style>
											<states>
												<selected_normal color={this.config.selectedColor || "%Color"}>
												</selected_normal>
											</states>
										</bar_style>
									</bar_series>
								</data_plot_settings>
								<controls>
									<label position="Fixed" anchor="RightTop" inside_dataplot={this.config.inside_dataplot || "false"} enabled={this.config.controlsLabelEnabled || "false"}>
<font family="Microsoft Yahei" bold="False" size="12" color="white" />
										<text>{this.config.controlsLabelText || ""}</text>
									</label>
								</controls>
								<chart_settings>
									<chart_background enabled="false">
									      <inside_margin left={this.config.leftMargin || "0"} top={this.config.topMargin || "0"} right={this.config.rightMargin || "0"} bottom="0"/>
									</chart_background>
									<data_plot_background enabled="false" />
										<title enabled={this.config.titleEnabled || "false"}>
											<text>{this.config.title || ''}</text>
<font family="Microsoft Yahei" bold="True" size="16" color="white" />
										</title>
										<subtitle enabled={this.config.subtitleEnabled || "false"}>
											<text>{this.config.subtitle || ''}</text>
<font family="Microsoft Yahei" bold="false" size="12" color="white" />
											<background enabled="false"/>
										</subtitle>
										<controls>
											<label position="Fixed" anchor={this.config.controlsLabelPosition || "RightTop"} inside_dataplot={this.config.inside_dataplot || "true"} enabled={this.config.controlsLabelEnabled || "false"}>
<font family="Microsoft Yahei" bold="False" size="12" color="white" />
												<text>{this.config.controlsLabelText || "单位：亿千瓦时"}</text>
											</label>
										</controls>
									<axes>
										<x_axis>
											<line color="white" />
											<major_tickmark color="white" />
											<title enabled="false" />
											<labels rotation={this.config.format_rotation || "0"}>
<font family="Microsoft Yahei" bold="false" size="12" color="white" />
											</labels>
											<major_grid enabled="false" />
											<minor_grid enabled="false" />
										</x_axis>
										<y_axis position={this.config.yAxisPosition || "Normal"}>
											<scale maximum_offset={this.config.maximumOffset||"0.1"} mode={this.config.y_axisScaleType || "Normal"} minimum = {this.config.y_axisScaleMinimum || "auto"} maximum = {this.config.y_axisScaleMaximum || "auto"} type={this.config.y_axisScaleType|| "Linear"} log_base={this.config.y_axisScaleLogbase|| "10"}/>
											<line color="white" enabled={this.config.y_axisLineEnabled||"true"}/>
											<major_tickmark color="white" enabled={this.config.y_axisMajorTickmarkEnabled||"true"}/>
											<minor_tickmark enabled={this.config.y_axisMinorTickmarkEnabled||"true"} />
											<title enabled="false" />
											<labels enabled={this.config.y_axisLabelsEnabled||"true"} >
<font family="Microsoft Yahei" bold="false" size="12" color={this.config.y_axisLabelsColor||"white"}/>
												<format>{"{%Value}{numDecimals:"}{this.config.format_Y1data||'2'}{"}"}{this.config.format_Y1Unit||''}</format>
											</labels>
											<major_grid enabled="false" />
											<minor_grid enabled="false" />
											<axis_markers>
											</axis_markers>
										</y_axis>
									</axes>
									<legend enabled={this.config.legendEnabled || "False"} ignore_auto_item="true" align="center" position={this.config.legendPosition || "right"} rows_padding="1" columns_padding="1">
<font family="Microsoft Yahei" bold="False" size="12" color="white">
										</font>
										<background enabled="false" />
										<title enabled="false" />
										<items>
											<item source="series" />
										</items>
									</legend>
								</chart_settings>
								<data>
								</data>
							</chart>
						</charts>
					</anychart>;
			return xml;
		}
			
		public function parseXML():XML {
			var wdbm:String=this.config.wdbm;
			var zbbm:Array=this.config.zbbm;
			var zbmc:Array=this.config.zbmc;
			var horizonLine_bm = this.config.horizonLine_bm || [];
			var horizonLine_mc = this.config.horizonLine_mc || [""];
			var horizonLine_position = this.config.horizonLine_position || "Center";
			var horizonLine_color = this.config.horizonLine_color || ['#00DFF3','#95A600'];
			var xmlStr:String="";
			var barXml:XML = getXML();
			var paramsColumns:Array = this.config.paramsColumns || [];
			//barXml.charts[0].chart[0].data[0]="";
			var seriesColor:Array= this.config.seriesColor || ['#00699C','#D2B993','#6B8E23'];
			if(data) {
			for(var i:int=0;i<zbbm.length;i++){
				xmlStr='<series name="'+zbmc[i]+'" type="Bar" color="'+seriesColor[i]+'">';
				var t:int = this.config.start_index || 0;
				var len:int = this.data.length - (this.config.end_index || 0);
				for(var j:int=t;j<len;j++){
					xmlStr+='<point name="'+this.data[j][wdbm]+'" y="' + (this.data[j][zbbm[i]] || 0) + '">';
					
					xmlStr += '<attributes>';
					for(var k:int = 0; k < paramsColumns.length; k++) {
						xmlStr += '<attribute name="' + paramsColumns[k] + '">' + this.data[j][paramsColumns[k]] + '</attribute>';
					}
					xmlStr += '</attributes></point>';
					
					
				}
				xmlStr+='</series>';
				barXml.charts[0].chart[0].data[0].appendChild(new XML(xmlStr));
			}
			//水平线
			if(this.data.length > 0) {
				var lineXmlStr:String = '<lines>';
				for(var i:int = 0; i < horizonLine_bm.length; i++) {
					lineXmlStr += '<line value="' + this.data[0][horizonLine_bm[i]] + '" color="' + horizonLine_color[i] + '" dashed="true" dash_length="5" space_length="5" display_under_data="false">' +
						'	<label enabled="True" multi_line_align="Center" position="'+horizonLine_position+'">' +
						'		<font bold="false" size="12" color="white"/>' +
						'		<format>' + (horizonLine_mc[i]||"") + '</format>' +
						'	</label>' +
						'</line>';
				}
				lineXmlStr += '</lines>';
				new XML(lineXmlStr);
				barXml.charts[0].chart[0].chart_settings[0].axes[0].y_axis[0].axis_markers[0].appendChild(new XML(lineXmlStr));
			}
			}
			return   barXml;
		}
				
	}
}